<?php
namespace app\common\controller;

/**
 * 处理积分流水类
 * Created by PhpStorm.
 * User: tyll
 * Date: 2019/11/27
 * Time: 11:18
 */
class DealIntegralRecord {

    /**
     * 新增积分流水
     * @param $openid
     * @param $sourceid
     * @param $source
     * community_patrol：社区巡店
     * no_community_patrol：未完成社区巡店
     * day_patrol：日常巡店
     * create_matter：创建事项
     * join_matter：参与事项
     * out_time_matter：超时事项
     * reply_copy：批复抄送
     * release_extend：发布推广工作
     * no_release_extend：未发布发布推广工作
     * release_daily：发布工作日志
     * release_active：发布活动开展
     * release_notice：发布通知公告
     * release_data：发布每日数据
     * release_account：发布日记账
     * release_circle：发布工作圈
     * circle_thumbs：工作圈点赞
     * circle_be_thumbs：工作圈被点赞
     * circle_comment：工作圈评论
     * circle_be_comment：工作圈被评论
     * class_learn：小课堂学习
     * write_remark：写备忘录
     * admin_operate：后台操作
     * @param $integral
     * @return int
     */
    public function add_integral_record($openid,$sourceid,$source,$integral){
        //处理积分收支描述
        $integral_desc = $this->deal_integral_record_content($sourceid,$source);

        $maps = [];
        $maps[] = ['openid','=',$openid];
        $maps[] = ['sourceid','=',$sourceid];
        $maps[] = ['source','=',$source];

        //新增积分流水
        $ir_detail = model('integral_record')->get_detail($maps,'id');
        if(empty($ir_detail)){
            $ir_data['openid'] = $openid;
            $ir_data['sourceid'] = $sourceid;
            $ir_data['source'] = $source;
            $ir_data['integral'] = $integral;
            $ir_data['integral_desc'] = $integral_desc;
            $r = model('integral_record')->insert($ir_data);
        }
        return 1;
    }

    /**
     * 处理积分收支描述
     * @param $sourceid
     * @param $source
     * @return int|string
     */
    public function deal_integral_record_content($sourceid,$source){
        $integral_desc = '';
        switch($source){
            case 'community_patrol':
                $cb_maps = [];
                $cb_maps[] = ['id','=',$sourceid];
                $cb_detail = model('cp_branch')->get_detail($cb_maps,'id,branchid,time_code');

                //查找分部
                $b_maps = [];
                $b_maps[] = ['id','=',$cb_detail['branchid']];
                $b_detail = model('branch')->get_detail($b_maps,'id,name');

                $maps = [];
                $maps[] = ['key','=','cp_time'];
                $conf_info = model('conf')->get_list($maps);
                $conf_info_new = [];
                foreach ($conf_info as $k => $v) {
                    $conf_info_new[$v['key']] = $v['value'];
                }
                $cp_time = json_decode($conf_info_new['cp_time'],true);
                $code_arr = array_column($cp_time,null,'code');
                $integral_desc = '完成：社区巡店-' . $b_detail['name'] . '-' .  $code_arr[$cb_detail['time_code']]['code_zw'];
                break;
            case 'no_community_patrol':
                $cb_maps = [];
                $cb_maps[] = ['id','=',$sourceid];
                $cb_detail = model('cp_branch')->get_detail($cb_maps,'id,branchid,time_code');

                //查找分部
                $b_maps = [];
                $b_maps[] = ['id','=',$cb_detail['branchid']];
                $b_detail = model('branch')->get_detail($b_maps,'id,name');

                $maps = [];
                $maps[] = ['key','=','cp_time'];
                $conf_info = model('conf')->get_list($maps);
                $conf_info_new = [];
                foreach ($conf_info as $k => $v) {
                    $conf_info_new[$v['key']] = $v['value'];
                }
                $cp_time = json_decode($conf_info_new['cp_time'],true);
                $code_arr = array_column($cp_time,null,'code');
                $integral_desc = '未完成：社区巡店-' . $b_detail['name'] . '-' .  $code_arr[$cb_detail['time_code']]['code_zw'];
                break;
            case 'day_patrol':
                $cb_maps = [];
                $cb_maps[] = ['id','=',$sourceid];
                $cf_detail = model('cp_floor')->get_detail($cb_maps,'id,branchid,floorid');

                //查找分部
                $b_maps = [];
                $b_maps[] = ['id','=',$cf_detail['branchid']];
                $b_detail = model('branch')->get_detail($b_maps,'id,name');

                //查找楼层
                $f_maps = [];
                $f_maps[] = ['id','=',$cf_detail['floorid']];
                $f_detail = model('floor')->get_detail($f_maps,'id,name');
                $integral_desc = '完成：日常巡店-' . $b_detail['name'] . '-' . $f_detail['name'];
                break;
            case 'create_matter':
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $m_detail = model('matter')->get_detail($maps,'id,name');
                $integral_desc = '创建事项：' . $m_detail['name'];
                break;
            case 'join_matter':
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $m_detail = model('matter')->get_detail($maps,'id,name');
                $integral_desc = '参与事项：' . $m_detail['name'];
                break;
            case 'out_time_matter':
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $m_detail = model('matter')->get_detail($maps,'id,name');
                $integral_desc = '事项超时：' . $m_detail['name'];
                break;
            case 'reply_copy':
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $c_detail = model('copy')->get_detail($maps,'id,name');
                $integral_desc = '批复抄送：' . $c_detail['name'];
                break;
            case 'release_extend':
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $wre_detail = model('work_record_extend')->get_detail($maps,'id,name');
                $integral_desc = '发布：推广工作-' . $wre_detail['name'];
                break;
            case 'no_release_extend':
                $integral_desc = '未发布：推广工作-' . $sourceid;
                break;
            case 'release_daily':
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $wrd_detail = model('work_record_daily')->get_detail($maps,'id,name');
                $integral_desc = '发布：工作日志-' . $wrd_detail['name'];
                break;
            case 'release_active':
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $wra_detail = model('work_record_active')->get_detail($maps,'id,name');
                $integral_desc = '发布：活动开展-' . $wra_detail['name'];
                break;
            case 'release_notice':
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $wrn_detail = model('work_record_notice')->get_detail($maps,'id,name');
                $integral_desc = '发布：通知公告-' . $wrn_detail['name'];
                break;
            case 'release_data':
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $wrd_detail = model('work_record_data')->get_detail($maps,'id,department');
                $integral_desc = '发布：每日数据-' . $wrd_detail['department'];
                break;
            case 'release_account':
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $wra_detail = model('work_record_account')->get_detail($maps,'id,department');
                $integral_desc = '发布：日记账-' . $wra_detail['department'];
                break;
            case 'release_circle':
                $integral_desc = '发布：工作圈';
                break;
            case 'circle_thumbs':
                $integral_desc = '点赞：工作圈';
                break;
            case 'circle_be_thumbs':
                $integral_desc = '被点赞：工作圈';
                break;
            case 'circle_comment':
                $integral_desc = '评论：工作圈';
                break;
            case 'circle_be_comment':
                $integral_desc = '被评论：工作圈';
                break;
            case 'class_learn':
                $integral_desc = '小课堂学习';
                break;
            case 'write_remark':
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $crw_detail = model('cp_region_ws')->get_detail($maps,'id,branchid,floorid,regionid,wsid');

                //查找工位
                $ws_maps = [];
                $ws_maps[] = ['id','=',$crw_detail['wsid']];
                $ws_detail = model('work_station')->get_detail($ws_maps,'id,number');

                //查找区域
                $r_maps = [];
                $r_maps[] = ['id','=',$crw_detail['regionid']];
                $r_detail = model('region')->get_detail($r_maps,'id,name');

                //查找楼层
                $f_maps = [];
                $f_maps[] = ['id','=',$crw_detail['floorid']];
                $f_detail = model('floor')->get_detail($f_maps,'id,name');

                //查找分部
                $b_maps = [];
                $b_maps[] = ['id','=',$crw_detail['branchid']];
                $b_detail = model('branch')->get_detail($b_maps,'id,name');
                $integral_desc = '备忘录：' . $b_detail['name'] . '-' . $f_detail['name'] . '-' . $r_detail['name'] . '-' . $ws_detail['number'];
                break;
            case 'admin_operate':
                $maps = [];
                $maps[] = ['id','=',$sourceid];
                $io_detail = model('integral_operate')->get_detail($maps,'id,explain');
                $integral_desc = '积分操作：' . $io_detail['explain'];
                break;
            default:
                return 0;
        }
        return $integral_desc;
    }
}